<?php

class usercp_events
{
	
	var $future;
	var $mess = '';
	var $myevents = Array();

	function usercp_events($future = true)
	{
		global $db;
		
		$this->future = $future;
		
		if(isset($_POST['removeEvent']) && isset($_POST['event']) && isset($_SESSION["valid_id"]))
		{
			$this->removeEvent();
		}
		$f = $future ? '>=' : '<';
		$order = $future ? 'ASC' : 'DESC';
		$q = "SELECT ch.c_id, ch.c_title, es_no, e_id, e_title, e_openSignUps, ca.c_id AS car_id, c_manufacturer, c_model, es_entryOwner,
                                    (SELECT t.t_id FROM " . TEAMS_TABLE . " t WHERE t.t_id = es_team LIMIT 0, 1) AS team_id,
                                    (SELECT t.t_name FROM " . TEAMS_TABLE . " t WHERE t.t_id = es_team LIMIT 0, 1) AS team_name,
                                    tr.t_fullTitle as track_name, d_title, ed_date as ed_dateuf, date_format(ed_date, '%M %e, %Y') as ed_date
                      FROM " . EVENTSIGNUPS_TABLE . ", " . EVENTS_TABLE . ", " . CHAMPIONSHIPS_TABLE . " ch, " . CARS_TABLE . " ca, " .
				DIVISIONS_TABLE . ", " . TRACKS_TABLE . " tr, " . EVENTDATES_TABLE . "
                      WHERE es_event = e_id AND es_division = d_id  AND e_championship = ch.c_id AND c_enabled = 1 AND
                            es_car = ca.c_id AND e_track = tr.t_id AND es_driver = {$_SESSION['valid_id']} AND
                            ed_event = e_id AND ed_division = d_id AND ed_date {$f} NOW()
                      ORDER BY ed_dateuf " . $order;

		$res = $db->sql_query($q);
		while( $row = $db->sql_fetchrow($res))
		{
			$this->myevents[] = $row;
		}
		
		
	}
	
	function render()
	{
		global $lang, $skin_dir;
		include_once 'includes/addMessage.php';
		
		if($this->mess != '')
		{
			$this->mess->render();
		}
		
		if($this->future)
		{
			$mess = new addMessage($lang['UserCPMyEventsFutureInfo'], $lang['UserCPMyEventsFuture']);
		}
		else
		{
			$mess = new addMessage($lang['UserCPMyEventsPastInfo'], $lang['UserCPMyEventsPast']);
		}
		$mess->render();
		
		
		include $skin_dir . 'tpl/tableStart.tpl';
		
		
		$eventid = -1;
		foreach($this->myevents as $event)
		{
			$eventid = $event['e_id'];
			$eventname = '<a href="championships.php?id=' . $event['c_id'] . '">' . StripSlashes($event['c_title']) . '</a><BR>';
			$eventname .= '<a href="events.php?id=' . $event['e_id'] . '">' . StripSlashes($event['e_title'] . ' ' . $event['track_name']) . '</a>';
			$entry =  $event['es_no'] == '' || $event['es_no'] == 0 ? '' : '#' . $event['es_no'] . ' ';
			$entry .= '<a href="cars.php?id=' . $event['car_id'] . '">' . StripSlashes($event['c_manufacturer'] . ' ' . $event['c_model']) . '</a><BR>';
			$entry .= $event['team_id'] == 0 ? $lang['Independent'] . ')' : '<a href="teams.php?id=' . $event['team_id'] . '">' .
                                    StripSlashes($event['team_name']) . '</a>';
			
			if(!$this->future)
			{
				$remStart = '<!--';
				$remEnd = '-->';
				$action = '';
			}
			else if(!$event['e_openSignUps'])
			{
				$remStart = '<!--';
				$remEnd = '-->';
				$action = '';
			}
			else
			{
				$remStart = '';
				$remEnd = '';
				$action = 'mycurrentevents';
			}
			
			include $skin_dir . 'tpl/usercpEventInfo.tpl';
		}
		
		if($eventid == -1)
		{
			echo '<tr><td class="tableButtonLeft">'. $lang['UserCPNoEntries'] . '</td></tr>';
		}
		
		include $skin_dir . 'tpl/tableEnd.tpl';
		
	}
	
	function removeEvent()
	{
		global $lang, $db;
		include_once('includes/addMessage.php');
	
				
		$q = "SELECT t_fullTitle, c_smallTitle, e_title, es_entryOwner
                      FROM " . EVENTSIGNUPS_TABLE . ", " . EVENTS_TABLE . ", " . CHAMPIONSHIPS_TABLE . ", " . TRACKS_TABLE . "
                      WHERE es_event = e_id AND e_championship = c_id AND e_track = t_id AND es_driver = {$_SESSION["valid_id"]}
                             AND e_id = {$_POST['event']}";
		$res = $db->sql_query($q);
		if( $rem = $db->sql_fetchrow($res))
		{
                        $qRemove = "DELETE FROM " . EVENTSIGNUPS_TABLE . " WHERE es_entryOwner = {$rem["es_entryOwner"]} AND es_event = {$_POST['event']}";
			$db->sql_query($qRemove);
			$this->mess = new addMessage(str_replace('{event}', $rem['c_smallTitle'] . ' ' . $rem['e_title'] . ' ' . $rem['t_fullTitle'], 
										$lang['UserCPMyEventsRemovedInfo']), $lang['UserCPMyEventsRemoved']);
		}	
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
		}
	}
}
?>